package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.CarEntity;
import com.sir.tao.carhire.service.CarService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author dyy
 * @data 2022/1/1 19:15
 * @description:
 */
@RestController
@Api(tags = "汽车管理接口")
@RequestMapping("car")
public class CarController {


    @Resource
    private CarService carService;

    /**
     * 新增一辆汽车
     * @param carEntity 汽车实体
     * @return 是否成功
     */
    @PostMapping("/add")
    @ApiOperation("新增一辆汽车")
    @ApiImplicitParam(name = "新增一辆汽车", value = "新增一辆汽车")
    public CommonResult<?> addCar(@RequestBody CarEntity carEntity){
        boolean i = carService.save(carEntity);
        if(i){
            return CommonResult.success();
        }else {
            return CommonResult.fail("新增失败");
        }
    }

    /**
     * 获取所有汽车
     * @return 所有的汽车实体
     */
    @GetMapping("/getAll")
    @ApiOperation("获取所有的汽车")
    @ApiImplicitParam(name = "获取所有的汽车", value = "获取所有的汽车")
    public CommonResult<?> getAllCar(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                     String search){
        List<CarEntity> list = carService.list(new LambdaQueryWrapper<CarEntity>()
                .like(!StringUtils.isEmpty(search),CarEntity::getNumberPlate,search));
        PageUtil page = PageUtil.doPage(pageNo,pageSize, list);
        return CommonResult.success(page);
    }

    /**
     * 删除一辆汽车
     * @param id id
     * @return 是否成功
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除一辆汽车")
    @ApiImplicitParam(name = "根据id删除一辆汽车", value = "根据id删除一辆汽车")
    public CommonResult<?> deleteCar(@PathVariable String id){
        boolean i = carService.removeById(id);
        if(i){
            return CommonResult.success();
        }else {
            return CommonResult.fail("删除失败");
        }
    }

    /**
     * 更新一辆汽车
     * @param carEntity 汽车实体
     * @return 是否成功
     */
    @PutMapping("/update")
    @ApiOperation("更新一辆汽车")
    @ApiImplicitParam(name = "更新一辆汽车", value = "更新一辆汽车")
    public CommonResult<?> update(@RequestBody CarEntity carEntity){
        boolean i = carService.updateById(carEntity);
        if(i){
            return CommonResult.success();
        }else {
            return CommonResult.fail("更新失败");
        }
    }




}
